package com.jdd.modules.parking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.common.constant.CacheConstant;
import com.jdd.modules.parking.entity.ParkInfo;
import com.jdd.modules.parking.mapper.ParkInfoMapper;
import com.jdd.modules.parking.service.IParkInfoService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 停车场信息
 * @Author: jdd
 * @Date: 2021-02-05
 * @Version: V1.0
 */
@Service
public class ParkInfoServiceImpl extends ServiceImpl<ParkInfoMapper, ParkInfo> implements IParkInfoService {
    @Resource
    private ParkInfoMapper parkInfoMapper;

    /**
     * 得到指定停车场的一条数据信息
     *
     * @return 停车场信息
     **/
    @Override
    public ParkInfo getParkInfoByParkCode() {
        return parkInfoMapper.selectList(null).get(0);
    }

    /**
     * 根据id获取指定车场信息
     *
     * @param id 车场id
     * @return 车场信息
     */
    @Override
    @Cacheable(value = CacheConstant.PARK_INFO_BY_ID, key = "#id")
    public ParkInfo findParkInfoById(String id) {
        return parkInfoMapper.selectById(id);
    }

    /**
     * 根据车场编号删除数据
     *
     * @return 删除状态
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_INFO_BY_ID, allEntries = true)
    })
    public Boolean removeByParkCode() {
        LambdaQueryWrapper<ParkInfo> queryWrapper = new LambdaQueryWrapper<>();
        return this.remove(queryWrapper);
    }

    /**
     * 更新指定的车场信息
     *
     * @param parkInfo 车场信息
     * @return 更新结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_INFO, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_INFO_BY_ID, allEntries = true)
    })
    public boolean updateEntity(ParkInfo parkInfo) {
        return parkInfoMapper.updateById(parkInfo) == 1;
    }

    /**
     * 保存指定的车场信息
     *
     * @param parkInfo 车场信息
     * @return 保存结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_INFO, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_INFO_BY_ID, allEntries = true)
    })
    public boolean saveEntity(ParkInfo parkInfo) {
        return parkInfoMapper.insert(parkInfo) == 1;
    }

    /**
     * 删除指定的车场信息
     *
     * @param id 车场信息id
     * @return 删除结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_INFO, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_INFO_BY_ID, allEntries = true)
    })
    public boolean deleteEntityById(String id) {
        return parkInfoMapper.deleteById(id) == 1;
    }

    /**
     * 批量删除指定的车场信息
     *
     * @param ids 车场信息id列表
     * @return 删除结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_INFO, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_INFO_BY_ID, allEntries = true)
    })
    public boolean deleteEntityByIds(List<String> ids) {
        return parkInfoMapper.deleteBatchIds(ids) == 1;
    }

    /**
     * 查询所有的停车场编号
     *
     * @return 所有的停车场编号
     **/
    @Override
    public List<String> getParkCodeList() {
        return parkInfoMapper.getParkCodeList();
    }

    /**
     * 查询数据库中的车场信息列表中的第一条车场信息
     *
     * @return 车场信息
     */
    @Override
    public ParkInfo getParkInfo() {
        List<ParkInfo> list = this.list();
        return list.get(0);
    }

    /**
     * 清空表数据
     *
     * @return true 清空成功
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = CacheConstant.PARK_INFO, allEntries = true),
            @CacheEvict(value = CacheConstant.PARK_INFO_BY_ID, allEntries = true)
    })
    public boolean removeAllInfo() {
        try {
            parkInfoMapper.removeAllInfo();
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 查询免费时间
     *
     * @return
     */
    @Override
    public ParkInfo getFreeMinutes() {
        return parkInfoMapper.getFreeMinutes();
    }

    /**
     * 根据ParkCode查詢車場信息
     *
     * @param parkCode
     * @return
     */
    @Override
    public ParkInfo findOneParkInfo(String parkCode) {
        LambdaQueryWrapper<ParkInfo> parkInfoQueryWrapper = new LambdaQueryWrapper<>();
        parkInfoQueryWrapper.eq(ParkInfo::getParkCode, parkCode);
        return this.getOne(parkInfoQueryWrapper);
    }
}
